package work.sajor.wechatpush.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @ClassName JiNianRi
 * @Description TODO
 * @Author ydzhao
 * @Date 2022/8/2 17:32
 */
public class JiNianRi {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 距离date还有多少天
     * @param date
     * @return
     */
    public static int before(String date) {
        int day = 0;
        try {
            long time = simpleDateFormat.parse(date).getTime() - System.currentTimeMillis();
            day = (int) (time / 86400000L);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day+1;
    }


    /**
     * 已经过去date多少天
     * @param date
     * @return
     */
    public static int after(String date) {
        int day = 0;
        try {
            long time = System.currentTimeMillis() - simpleDateFormat.parse(date).getTime();
            day = (int) (time / 86400000L);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }


    private static int nongBefore(String shengRi) {
        Date date = null;
        try {
            date = simpleDateFormat.parse(shengRi);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        int day= calendar.get(Calendar.DATE);
        int[] ints = LunarCalendarUtil.lunarToSolar(year,month,day, false);
        System.out.println("Arrays.toString(ints) = " + Arrays.toString(ints));

        calendar.clear();
        calendar.set(ints[0], ints[1]-1, ints[2]);
        String format = simpleDateFormat.format(calendar.getTime());
        System.out.println("format = " + format);
        int before = before(format);
        return before;
    }
    public static int getJieHun(String jieHun) {
        return before(jieHun);
    }

    public static int getLinZhen(String linZheng) {
        return before(linZheng);
    }

    public static int getLianAi(String lianAi) {
        return after(lianAi);
    }

    public static int getShengRi(String shengRi){
        return before(shengRi);
    }
    public static int getNongShengRi(String shengRi){
        return nongBefore(shengRi);
    }
    public static void main(String[] args) throws ParseException {
        int i = nongBefore("2022-08-20");
        System.out.println("i = " + i);

//        System.out.println(getJieHun());
    }


}
